<?php
namespace Home\Controller;
use Think\Controller;
class IndexController extends CommonController {
    public function index(){
    	$condition['answer_num'] = array('GT',0);
    	$ans_max = M('user')->field('uid,nickname,face,answer_num')->where($condition)->order('answer_num desc')->limit(12)->select();
    	//var_dump($ans_max);exit;
    	$this->assign('ans_max',$ans_max);

    	$sql_top = "select t1.*,t2.uid,t2.nickname,t2.face,t3.cname from question t1 left join user t2 on t1.uid=t2.uid left join category t3 on t1.cid=t3.cid where t1.level='2' order by create_time desc";
		$top = M()->query($sql_top);
		$this->assign('top',$top);
		//var_dump($top);exit;
		
		$sql_main = "select t1.*,t2.uid,t2.nickname,t2.face,t3.cname from question t1 left join user t2 on t1.uid=t2.uid left join category t3 on t1.cid=t3.cid where t1.level<>'2' order by create_time desc";
		$main = M()->query($sql_main);
		$this->assign('main',$main);

	    $rows = M('question')->field('title,answer_num,qid')->order('answer_num desc')->limit(15)->select();
	    $this->assign('rows',$rows);
	    $this->assign('title','首页');
	    $this->display();
    }

    public function all(){
    	$sql_top = "select t1.*,t2.uid,t2.nickname,t2.face,t3.cname from question t1 left join user t2 on t1.uid=t2.uid left join category t3 on t1.cid=t3.cid where t1.level='2' order by create_time desc";
		$top = M()->query($sql_top);
		$this->assign('top',$top);
		
		//分页开始
		$condition['level']=array('NEQ','2');
		$count = M('question')->where($condition)->count();
		//var_dump($count);exit;
		$where = "t1.level<>'2'";
		$this->page($where,$count);
		$this->assign('title','开发者讨论区');
    	$this->display();
    }
    public function unsolve(){
		$map['level']  = array('neq','2');
		$map['status']='未结贴';
		$count = M('question')->where($map)->count();
		//var_dump($count); exit;
		$where="t1.status='未结贴' and t1.level<>'2'";
    	$this->page($where,$count);
    	$this->assign('title','开发者讨论区');
    	$this->display('all');
    }
    public function solve(){
    	$count = M('question')->where("status='已采纳'")->count();
    	$where = "t1.status='已采纳'";
    	//var_dump($count);exit;
    	$this->page($where,$count);
    	$this->assign('title','开发者讨论区');
    	$this->display('all');
    }
    public function wonder(){
    	$count = M('question')->where("level='1'")->count();
    	$where = "t1.level='1'";
    	$this->page($where,$count);
    	$this->assign('title','开发者讨论区');
    	$this->display('all');
    }
    public function myquestion(){
    	$uid = $_SESSION['uid'];
    	$count = M('question')->where(array('uid'=>$uid))->count();
    	$where = "t1.uid='$uid'";
    	//var_dump($count);exit;
    	$this->page($where,$count);
    	$this->assign('title','开发者讨论区');
    	$this->display('all');
    }

    public function page($where='',$count=0){
    	// 查询满足要求的总记录数
		$Page = new \Think\Page($count,5);// 实例化分页类 传入总记录数和每页显示的记录数(25)
		$show = $Page->show();// 分页显示输出
		// 进行分页数据查询 注意limit方法的参数要使用Page类的属性
		$sql_main = "select t1.*,t2.uid,t2.nickname,t2.face,t3.cname from question t1 left join user t2 on t1.uid=t2.uid left join category t3 on t1.cid=t3.cid where $where order by create_time desc limit ".$Page->firstRow.",".$Page->listRows;
		$main = M()->query($sql_main);
		$this->assign('main',$main);
		/*$list = $User->where('status=1')->order('create_time')->limit($Page->firstRow.','.$Page->listRows)->select();*/
		$this->assign('list',$list);// 赋值数据集
		$this->assign('pages',$show);// 赋值分页输出
		//分页结束
    }

    
}